﻿//TODO: Test: FogFilter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Tries to add some fog to a given bitmap
    /// </summary>
    public class FogFilter : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FogFilter"/> class.
        /// </summary>
        public FogFilter()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FogFilter"/> class.
        /// </summary>
        /// <param name="_fogFilter">The _fog filter.</param>
        internal FogFilter(FogFilter _fogFilter)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            float fFactor = 1.1f;
            int nOffset = 0;
            UnsafeBitmap bitmap = _bitmap;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            int nR = 0;
            int nG = 0;
            int nB = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x,y);
                    nR = color.R;
                    nG = color.G;
                    nB = color.B;

                    nR = (int)(nR * fFactor) + nOffset;
                    nG = (int)(nG * fFactor) + nOffset;
                    nB = (int)(nB * fFactor) + nOffset;
                    if (nR > 255) nR = 255;
                    if (nG > 255) nG = 255;
                    if (nB > 255) nB = 255;
                    color = Color.FromArgb(nR, nG, nB);
                    bitmap.SetPixel(x, y, color);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new FogFilter(this);
        }
    }
}
